package com.xunk.equipment.service.ruying;

import com.xunk.common.core.exception.CustomException;
import com.xunk.common.core.utils.DataUtils;
import com.xunk.common.redis.service.RedisService;
import com.xunk.equipment.domain.EquipmentDO;
import com.xunk.equipment.service.BaseService;
import com.xunk.equipment.service.IGasAlarmService;
import com.xunk.equipment.vo.response.GasAlarmInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xunk.common.core.domain.EquipmentResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.service.ruying
 * @ClassName: SmokeAlarmServiceImpl
 * @Author: yangwendong
 * @Description: 如影烟雾报警器
 * @Date: 2021/3/29 20:52
 * @Version: 1.0
 */
@Slf4j
@Service("ruying-gas-sensor")
public class GasAlarmService implements IGasAlarmService {

    @Autowired
    RedisService redisService;

    @Autowired
    BaseService baseService;

    @Autowired
    RuYingControlService ruYingControlService;

    //  硬件信息
    private static final String MODEL_ID = ".1.5";
    private static final String SERIAL_NUMBER_ID = ".1.7";
    private static final String FIRMWARE_REVISION_ID = ".1.2";

    @Override
    public EquipmentResponse getEquipmentInfo(EquipmentDO equipmentDO) {
        // 查询烟雾报警器的状态、告警状态、当前电量
        // 查询该设备的缓存状态
        String deviceSn = equipmentDO.getDeviceSn();
        Long deptId = equipmentDO.getDeptId();
        Map<String, String> cacheMap = ruYingControlService.getShadowByDeviceSn(deviceSn, deptId);
        // 报警器的探测状态，pid = did.10.11 ,value值为0或者1
        String detectState = deviceSn + ".10.11";
        String detectStateValue = cacheMap.get(detectState);

        // 故障状态
        String faultState = deviceSn + ".10.14";
        String faultStateValue = cacheMap.getOrDefault(faultState,"0");

        // 低电量状态
        String lowBatteryState = deviceSn + ".17.20";
        String lowBatteryStateValue = cacheMap.getOrDefault(lowBatteryState,"0");

        // 报警器的电池电量 pid = did.17.18，value值为0-100之间步长为1的数
        String electricQuantityState = deviceSn + ".17.18";
        String electricQuantityStateValue = cacheMap.get(electricQuantityState);
        GasAlarmInfoResponse response = new GasAlarmInfoResponse();
        response.setDeviceSn(deviceSn);
        response.setFaultState(Integer.valueOf(faultStateValue));
        response.setDetectedState(Integer.valueOf(detectStateValue));
        response.setLowBattery(Integer.valueOf(lowBatteryStateValue));
        response.setBatteryLevel(DataUtils.isNotEmpty(electricQuantityStateValue)? Integer.valueOf(electricQuantityStateValue):null);
        response.setElectricQuantityRange(electricQuantityRange);

        response.setModel(cacheMap.get(deviceSn.concat(MODEL_ID)));
        response.setFirmWare(cacheMap.get(deviceSn.concat(FIRMWARE_REVISION_ID)));
        response.setSerialNumber(cacheMap.get(deviceSn.concat(SERIAL_NUMBER_ID)));
        response.setSupplier(equipmentDO.getSupplier());
        response.setCandidateModel(baseService.getCandidateModel(equipmentDO.getDeviceBaseId()));
        return response;
    }

    private Map<String,String> electricQuantityRange = new HashMap<String,String>(3){{
        put("min","0");
        put("max","100");
        put("step","1");
    }};

    @Override
    public Boolean control(EquipmentDO equipmentDO, String type, String value) {
        throw new CustomException("燃气报警器不支持控制");
    }
}
